﻿using System;
using System.Collections.ObjectModel;
using Mal.Balades.Admin.Services;

namespace Mal.Balades.Admin.ViewModels.FeedViewModels
{
    public class FeedViewModel : DataViewModelBase
    {
        #region Fields
        private IFeed feed;
        private int feedId;
        private string name;
        private string url;
        private double? latitude;
        private double? longitude;
        private DateTime? lastUpdate;
        private int numberOfFeedItems;
        private bool isUpdating;
        private bool isSelected;
        private FeedItemStatus status = FeedItemStatus.None;

        #endregion

        #region Construction / Destruction
        public FeedViewModel()
        {

        }

        public FeedViewModel(IFeed feed)
        {
            this.Feed = feed;
        }
        #endregion

        #region enum, struct, class
        public enum FeedItemStatus
        {
            None,
            InProgress,
            Pending,
            Error,
            Done
        }
        #endregion

        #region Properties
        public bool IsUpdating
        {
            get
            {
                return this.isUpdating;
            }

            set
            {
                if (this.isUpdating != value)
                {
                    this.isUpdating = value;
                    RaisePropertyChanged("IsUpdating");
                }
            }
        }

        public int NumberOfFeedItems
        {
            get
            {
                return this.numberOfFeedItems;
            }

            set
            {
                if (this.numberOfFeedItems != value)
                {
                    this.numberOfFeedItems = value;
                    RaisePropertyChanged("NumberOfFeedItems");
                }
            }
        }

        public DateTime? LastUpdate
        {
            get
            {
                return this.lastUpdate;
            }

            set
            {
                if (this.lastUpdate != value)
                {
                    this.lastUpdate = value;
                    RaisePropertyChanged("LastUpdate");
                }
            }
        }

        public double? Longitude
        {
            get
            {
                return this.longitude;
            }

            set
            {
                if (this.longitude != value)
                {
                    this.longitude = value;
                    RaisePropertyChanged("Longitude");
                }
            }
        }

        public double? Latitude
        {
            get
            {
                return this.latitude;
            }

            set
            {
                if (this.latitude != value)
                {
                    this.latitude = value;
                    RaisePropertyChanged("Latitude");
                }
            }
        }

        public string Url
        {
            get
            {
                return this.url;
            }

            set
            {
                if (this.url != value)
                {
                    this.url = value;
                    RaisePropertyChanged("Url");
                }
            }
        }

        public string Name
        {
            get
            {
                return this.name;
            }

            set
            {
                if (this.name != value)
                {
                    this.name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        public int FeedId
        {
            get
            {
                return this.feedId;
            }

            private set
            {
                if (this.feedId != value)
                {
                    this.feedId = value;
                    RaisePropertyChanged("FeedId");
                }
            }
        }

        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }

            set
            {
                if (this.isSelected != value)
                {
                    this.isSelected = value;
                    RaisePropertyChanged("IsSelected");
                }
            }
        }

        public FeedItemStatus Status
        {
            get
            {
                return this.status;
            }

            set
            {
                if (this.status != value)
                {
                    this.status = value;
                    RaisePropertyChanged("Status");
                }
            }
        }

        private IFeed Feed
        {
            get
            {
                return this.feed;
            }

            set
            {
                this.feed = value;

                this.FeedId = this.feed.FeedId;
                this.LastUpdate = this.feed.LastUpdate;
                this.Latitude = this.feed.Latitude;
                this.Longitude = this.feed.Longitude;
                this.Name = this.feed.Name;
                this.Url = this.feed.Url;
                this.NumberOfFeedItems = this.feed.NumberOfFeedItems;
            }
        }
        #endregion

        #region Methods
        public static ObservableCollection<FeedViewModel> Convert(ReadOnlyCollection<IFeed> feeds)
        {
            ObservableCollection<FeedViewModel> results = new ObservableCollection<FeedViewModel>();

            foreach (IFeed item in feeds)
            {
                results.Add(new FeedViewModel(item));
            }

            return results;
        }

        protected override bool GetIsChanged()
        {
            if (this.feed == null)
            {
                return true;
            }

            if (this.Latitude != this.feed.Latitude)
            {
                return true;
            }

            if (this.Longitude != this.feed.Longitude)
            {
                return true;
            }

            if (this.Name != this.feed.Name)
            {
                return true;
            }

            if (this.Url != this.feed.Url)
            {
                return true;
            }

            return false;
        }
        #endregion

        #region Events

        #endregion
    }
}
